{extend name="admin@publics/base"/}
{block name="title"}模块管理{/block}
{block name="body"}

<script>
//页面变量
_PAGE['adminModule'] = {
	"status":[{"text":"启用","value":"1"},{"text":"禁用","value":"0"},{"text":"未安装","value":"-1"}]
};
/*==========控制器==========*/
Ext.define('admin.module.CURDViewController', {
	extend: 'Tg.tx.CURDViewController',
	alias: 'controller._adminModuleController',
	//行_安装
    _ColumInstall:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.install');
        var _Select = _Grid.getStore().getAt(_Rindex);
        Ext.Msg.confirm('确认安装','是否确认安装?',function(op){
			if(op == 'yes'){
				var _Params={};
				_Params['name']=_Select.get('name');
				var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store})
			}
		});
    },
	//行_卸载
    _ColumUninstall:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.uninstall');
        var _Select = _Grid.getStore().getAt(_Rindex);
        Ext.Msg.confirm('确认卸载','是否确认卸载?',function(op){
			if(op == 'yes'){
				var _Params={};
				_Params['name']=_Select.get('name');
				var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store})
			}
		});
    },
	//行_配置
    _ColumSetting:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.setting');
		var _Ext = _VM.get('_Ext.setting');
		var _Pk = _VM.get('_Pk');
        var _Select = _Grid.getStore().getAt(_Rindex);
		//获取 配置表单数据
		Ext.Ajax.request({
			url: _Url+'.js',
			method: 'get',
			params: {'id': _Select.get('name')},
			async: false, 
			success: function (response, opts) {
				_Rec = _TObj(response.responseText);
				if (parseInt(_Rec.code) == 200) {
					_FormField =  eval('(' + _Rec.data + ')');
				} else {
					Ext.Msg.alert('错误', _Rec.msg);
					return false;
				}
			},
			failure: function (response, opts) {
				Ext.MessageBox.alert('错误', '服务器异常');
				return false;
			}
		});
        var _Win = Ext.create('Tg.tx.Window',{
            requires: [
                'admin.plug.CURDViewController',
                'admin.plug.CURDViewModel',
            ],
            controller: '_AdminPlugController',
            viewModel: '_AdminPlugViewModel',
            title:'配置插件',
            items:{
                xtype:'CURDForm',
                items:_FormField
            },
            bbar: new Ext.Toolbar({height: 45,style: 'background: #5fa2dd;',items: [	
                '->',
                {
                    text:'保存',
                    handler:function(_SubBtn){
                        var _Form	= _SubBtn.up("window").down("form").getForm();
                        var _Params	= _Form.getValues();
						var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store});
                        if(_Return){
                        	_SubBtn.up("window").close();
                        }
                        
                    }
                },
                {
                    text:'取消',
                    handler:function(_CloseBtn){
                        _CloseBtn.up("window").close();
                    }
                }
            ]})
        });
		var _Params={};
		_Params[_Pk]=_Select.get(_Pk);
        Ext.Ajax.request({
            url: _Url,
            method: 'get',
            params:_Params,
            async: false,
            success: function (response, options) {
                var _Rec = _TObj(response.responseText);
                if (parseInt(_Rec.code) == 200) {
                	_Win.down("form").getForm().setValues(_Rec.data);
                } else {
                    Ext.Msg.alert('错误', _Rec.msg);
                }
            },
            failure: function () {
            }
        });
    },
    //行_启用/禁用
    _ColumSta:function(_Grid, _Rindex, _Cindex) {
		var _Me	= this;
		var _V	= _Me.getView();
		var _VM	= _V.getViewModel();
		var _Store = _VM.get('_MainStore');
		var _Url = _VM.get('_Url.sta');
		var _Pk = _VM.get('_Pk');
        var _Select = _Grid.getStore().getAt(_Rindex);
		var _Params={};
		_Params[_Pk]=_Select.get(_Pk);
		_Params['sta']=(_Select.get('status') == 1)?0:1;
        var _Return	= Ext.create('Tg.tx.Action').doAction({params:_Params,url:_Url,store:_Store})
    },
	//行双击
	_ItemDbClick:function(){
	}
});
/*==========模型==========*/
Ext.define('admin.module.CURDViewModel', {
	extend: 'Tg.tx.CURDViewModel',
	alias: 'viewmodel._adminModuleViewModel',
    //Data
    data:{
    	//权限
        _Auth:{
        	index	: {if condition="_CheckAuth('admin/Module/index')"}true{else/}false{/if},
        	serch	: false,
        	setting	: {if condition="_CheckAuth('admin/Module/setting')"}true{else/}false{/if},
        	install	: {if condition="_CheckAuth('admin/Module/install')"}true{else/}false{/if},
        	uninstall	: {if condition="_CheckAuth('admin/Module/uninstall')"}true{else/}false{/if},
        	sta		: {if condition="_CheckAuth('admin/Module/sta')"}true{else/}false{/if},
        },
        _Url:{
        	index	: _PATH['site']+'/admin/module/index',
        	setting	: _PATH['site']+'/admin/module/setting',
        	install	: _PATH['site']+'/admin/module/install',
        	uninstall	: _PATH['site']+'/admin/module/uninstall',
        	sta		: _PATH['site']+'/admin/module/sta',
        },
        //操作视图
        _Ext:{
        	index	: 'admin.module.index',
        	setting	: 'admin.module.setting',
        },
		_Pk:'id',
    },
    //Store
	stores: {
		_MainStore: {
			sorters: [{property: 'id',direction: 'desc'}],
			pageSize: 20,
			remoteSort: true,
			remoteFilter:true,
			proxy: {
				type: 'ajax',
				api: {read:_PATH['site']+'/admin/module/index'},
				actionMethods:{read: "POST"},
				reader: {type: 'json',rootProperty: 'rows',totalProperty: 'total'}
			},
		}
	},
});
/*==========视图==========*/
//列表
Ext.define('admin.module.index', {
	extend: 'Tg.tx.CURDList',
	alias: 'widget.admin_Module_index',
	requires: [
		'admin.module.CURDViewController',
		'admin.module.CURDViewModel',
	],
	controller: '_adminModuleController',
	viewModel: '_adminModuleViewModel',
    _TbarLBefore:[
        {xtype: 'label',text: '模块管理',itemId:'_Tit',style: 'font-size:16px; padding-right:10px;margin-right: 10px;border-right: dotted 1px #ccc;'},
    ],
    items:[
		{
			xtype:'CURDListGrid',
			bind:{store:'{_MainStore}'},
			columns:[
        		{xtype:"actioncolumn",header:"安装",width: 50,align:'center',locked: true,handler: '_ColumInstall',bind:{hidden:'{!_Auth.install}'},getClass : function(v, metadata, record){
					if(!(record.data.status == -1)){
						return 'x-hidden';
					}else{
						return 'tf tf-install';
					}
				}},
        		{xtype:"actioncolumn",header:"卸载",width: 50,align:'center',locked: true,handler: '_ColumUninstall',bind:{hidden:'{!_Auth.uninstall}'},getClass : function(v, metadata, record){
					if(!(record.data.status != -1)){
						return 'x-hidden';
					}else{
						return 'tf tf-uninstall';
					}
				}},
        		{xtype:"actioncolumn",header:"配置",width: 50,align:'center',locked: true,handler: '_ColumSetting',bind:{hidden:'{!_Auth.setting}'},getClass : function(v, metadata, record){
					if(!(record.data.status != -1) || record.data.is_conf != 1){
						return 'x-hidden';
					}else{
						return 'tf tf-setting';
					}
				}},
        		{xtype:"actioncolumn",header:"状态",width: 50,align:'center',locked: true,handler: '_ColumSta',bind:{hidden:'{!_Auth.sta}'},getClass : function(v, metadata, record){
					if(record.data.status == 1){
						//禁用
						return 'tf tf-false';
					}else if(record.data.status == 0){
						//启用
						return 'tf tf-true';
					}else{
						return 'x-hidden';
					}
				}},
				{dataIndex:'id', header :'主键', width :60, hidden:false, locked:true, sortable:true },
				{header : "模块名称[模块][版本] - 包",width :220,locked:false,renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {
					return record.data.title+'['+record.data.name+']['+record.data.version+']<br/>'+record.data.use;
				}},
				{header : "作者",width :280,locked:false,renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {
					return record.data.author+'<br/>网址：<a href="'+record.data.author_site+'" target="_blank">'+record.data.author_site+'</a><br/>邮箱：'+record.data.author_email;
				}},
				{dataIndex:'status', header :'状态', width :80, hidden:false, locked:false, sortable:true, renderer: function (value, cellmeta, record, rowIndex, columnIndex, store) {return _Res_Val(_PAGE['adminModule'],'status', value);}},
				{dataIndex:'create_time', header :'创建时间', width :110, hidden:true, locked:false, sortable:true },
				{dataIndex:'update_time', header :'更新时间', width :110, hidden:true, locked:false, sortable:true },
			],
		}
	],
});

//实例化
Ext.application({
	name: 'admin',
	extend: 'Ext.app.Application',
	autoCreateViewport: 'admin.module.index'
});
</script>

{/block}
